
// testclientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testclient.h"
#include "testclientDlg.h"
#include "potocol.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CtestclientDlg dialog




CtestclientDlg::CtestclientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CtestclientDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CtestclientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CtestclientDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_CONNECT, &CtestclientDlg::OnBnClickedConnect)
	ON_BN_CLICKED(IDC_LOGIN, &CtestclientDlg::OnBnClickedLogin)
	ON_BN_CLICKED(IDC_AddFriend, &CtestclientDlg::OnBnClickedAddfriend)
	ON_BN_CLICKED(IDC_FriendReq, &CtestclientDlg::OnBnClickedFriendreq)
	ON_BN_CLICKED(IDC_PlatPrepare, &CtestclientDlg::OnBnClickedPlatprepare)
	ON_BN_CLICKED(IDC_CanclePlatPre, &CtestclientDlg::OnBnClickedCancleplatpre)
	ON_BN_CLICKED(IDC_PlatInfoReq, &CtestclientDlg::OnBnClickedPlatinforeq)
	ON_BN_CLICKED(IDC_Chat, &CtestclientDlg::OnBnClickedChat)
	ON_BN_CLICKED(IDC_BTN_ADD_FRIEND_BYID, &CtestclientDlg::OnBnClickedBtnAddFriendByid)
	ON_BN_CLICKED(IDC_BTN_BULID_REQ, &CtestclientDlg::OnBnClickedBtnBulidReq)
	ON_BN_CLICKED(IDC_BTN_GAME_END_REQ, &CtestclientDlg::OnBnClickedBtnGameEndReq)
	ON_BN_CLICKED(IDC_BTN_PALYER_FRIEND_REQ, &CtestclientDlg::OnBnClickedBtnPalyerFriendReq)
	ON_BN_CLICKED(IDC_BTN_LEVELUP_REQ, &CtestclientDlg::OnBnClickedBtnLevelupReq)
	ON_BN_CLICKED(IDC_BUTN_BULIDSOLD, &CtestclientDlg::OnBnClickedButnBulidsold)
	ON_BN_CLICKED(IDC_BTN_REGISTER, &CtestclientDlg::OnBnClickedBtnRegister)
	ON_BN_CLICKED(IDC_BTN_RESTERT, &CtestclientDlg::OnBnClickedBtnRestert)
	ON_BN_CLICKED(IDC_BTN_RESTART_ACK, &CtestclientDlg::OnBnClickedBtnRestartAck)
	ON_BN_CLICKED(IDC_BUTTON_SND_REQ, &CtestclientDlg::OnBnClickedButtonSndReq)
	ON_BN_CLICKED(IDC_BTN_RANK_REQ, &CtestclientDlg::OnBnClickedBtnRankReq)
END_MESSAGE_MAP()


// CtestclientDlg message handlers

BOOL CtestclientDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	m_strbyChannel = "";
	m_strChatInfo = "";
	m_strUserId = "";
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CtestclientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CtestclientDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CtestclientDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CtestclientDlg::OnBnClickedConnect()
{
	// TODO: Add your control notification handler code here
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	wVersionRequested = MAKEWORD( 1, 1 );
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 )
	{
		MessageBox("Net Init Failed!");
		return;
	}

	if (!m_oNet.Init("127.0.0.1", 6000))
	{
		MessageBox("Net Init Failed!");
		return;
	}

	MessageBox("Net Init OK!");
}

void CtestclientDlg::OnBnClickedLogin()
{
	// TODO: Add your control notification handler code here
	CString	strName = "";
	CString strPsw = "";
	GetDlgItem(IDC_EDIT_LOGIN_NAME)->GetWindowText(strName);
	GetDlgItem(IDC_EDIT_LOGIN_PSW)->GetWindowText(strPsw);

	stMsgUserLoginReq stMsg;
	memcpy(stMsg.szUserName, strName.GetBuffer(0), strName.GetLength());
	memcpy(stMsg.szPsw, strPsw.GetBuffer(0), strPsw.GetLength());
	m_oNet.SendMsg((stMsgHead*)&stMsg);	
}

void CtestclientDlg::OnBnClickedAddfriend()
{
	// TODO: Add your control notification handler code here
	stMsgUserAddFriendByNameReq stMsg;
	CString strName = "";
	GetDlgItem(IDC_EDIT_FRIEND_NAME)->GetWindowText(strName);
	memcpy(stMsg.szUserName, strName.GetBuffer(0), strName.GetLength());
	m_oNet.SendMsg((stMsgHead*)&stMsg);	
}

void CtestclientDlg::OnBnClickedFriendreq()
{
	// TODO: Add your control notification handler code here
	stUserFriendInfoReq stMsg;
	m_oNet.SendMsg((stMsgHead*)&stMsg);	
}

void CtestclientDlg::OnBnClickedPlatprepare()
{
	// TODO: Add your control notification handler code here
	CString strMapId = "";
	GetDlgItem(IDC_EDIT_PLAT_MAP_ID)->GetWindowText(strMapId);
	stMsgPlatMapPrepareReq stMsg;
	stMsg.dwMapId = atoi(strMapId.GetBuffer(0));
	m_oNet.SendMsg((stMsgHead*)&stMsg);	
}

void CtestclientDlg::OnBnClickedCancleplatpre()
{
	// TODO: Add your control notification handler code here
	CString strMapId = "";
	GetDlgItem(IDC_EDIT_PLAT_MAP_ID)->GetWindowText(strMapId);
	stMsgMapCanclePrepareReq stMsg;
	stMsg.dwMapId = atoi(strMapId.GetBuffer(0));;
	m_oNet.SendMsg((stMsgHead*)&stMsg);	
}

void CtestclientDlg::OnBnClickedPlatinforeq()
{
	// TODO: Add your control notification handler code here
	stMsgPlatInfoReq stMsg;
	m_oNet.SendMsg((stMsgHead*)&stMsg);	
}

void CtestclientDlg::OnBnClickedChat()
{
	// TODO: Add your control notification handler code here
	
	GetDlgItem(IDC_EDIT_CHATCHANNEL)->GetWindowText(m_strbyChannel);
	GetDlgItem(IDC_EDIT_CHATINFO)->GetWindowText(m_strChatInfo);
	GetDlgItem(IDC_EDIT_USERID)->GetWindowText(m_strUserId);
	stMsgUserSndChatReq stMsg;
	stMsg.byChannel = atoi(m_strbyChannel.GetBuffer(0));
	stMsg.dwUserId = atoi(m_strUserId.GetBuffer(0));
	memcpy(stMsg.szChatInfo, m_strChatInfo.GetBuffer(0), m_strChatInfo.GetLength());
	m_oNet.SendMsg((stMsgHead*)&stMsg);
}

void CtestclientDlg::OnBnClickedBtnAddFriendByid()
{
	// TODO: Add your control notification handler code here
	CString strFriendId = "";
	GetDlgItem(IDC_EDIT_FRIEND_ID)->GetWindowText(strFriendId);
	stMsgUserAddFriendReq stMsg;
	stMsg.dwUserId = atoi(strFriendId.GetBuffer(0));
	m_oNet.SendMsg((stMsgHead*)&stMsg);
}

void CtestclientDlg::OnBnClickedBtnBulidReq()
{
	// TODO: Add your control notification handler code here
	CString strObjId = "";
	CString	strPosX = "";
	CString	strPosY = "";

	GetDlgItem(IDC_EDIT_BULID_ID)->GetWindowText(strObjId);
	GetDlgItem(IDC_EDIT_POSX)->GetWindowText(strPosX);
	GetDlgItem(IDC_EDIT_POSY)->GetWindowText(strPosY);

	stMsgBulidReq	stMsg;
	stMsg.dwBulidTypeId = 10;
	stMsg.qwBulidingObjId = atoi(strObjId.GetBuffer(0));
	stMsg.nPosX = atoi(strPosX.GetBuffer(0));
	stMsg.nPosY = atoi(strPosY.GetBuffer(0));
	m_oNet.SendMsg(&stMsg);
}

void CtestclientDlg::OnBnClickedBtnGameEndReq()
{
	// TODO: Add your control notification handler code here
	CString strVictory = "";
	GetDlgItem(IDC_EDIT_BYVICTORY)->GetWindowText(strVictory);
	stMsgMapRoomEndReq stMsg;
	stMsg.byVictory = atoi(strVictory.GetBuffer(0));
	m_oNet.SendMsg(&stMsg);
}

void CtestclientDlg::OnBnClickedBtnPalyerFriendReq()
{
	// TODO: Add your control notification handler code here
	CString strUserId = "";
	GetDlgItem(IDC_EDIT_PREPARE_FRIEND_iD)->GetWindowText(strUserId);
	CString strMapId = "";
	GetDlgItem(IDC_EDIT_PLAT_MAP_ID)->GetWindowText(strMapId);

	stMsgInviteFriendPlayGameReq stMsg;
	stMsg.dwFriendId = atoi(strUserId.GetBuffer(0));
	stMsg.dwMapId = atoi(strMapId.GetBuffer(0));
	m_oNet.SendMsg((stMsgHead*)&stMsg);
}

void CtestclientDlg::OnBnClickedBtnLevelupReq()
{
	// TODO: Add your control notification handler code here
	CString strObjId = "";
	GetDlgItem(IDC_EDIT_LEVELUP_BULID_ID)->GetWindowText(strObjId);
	stMsgBulidLevelUpReq stMsg;
	stMsg.qwBulidingObjId = atoi(strObjId.GetBuffer(0));
	m_oNet.SendMsg(&stMsg);
}

void CtestclientDlg::OnBnClickedButnBulidsold()
{
	// TODO: Add your control notification handler code here
	CString strObjId = "";
	GetDlgItem(IDC_EDIT_SOLD_BULID_ID)->GetWindowText(strObjId);
	stMsgBulidSoldReq stMsg;
	stMsg.qwBulidingObjId = atoi(strObjId.GetBuffer(0));
	m_oNet.SendMsg(&stMsg);
}

void CtestclientDlg::OnBnClickedBtnRegister()
{
	// TODO: Add your control notification handler code here
	CString strName = "";
	CString strPsw = "";
	CString strIconId = "";
	GetDlgItem(IDC_EDIT_REGISTER_NAME)->GetWindowText(strName);
	GetDlgItem(IDC_EDIT_REGISTER_PSW)->GetWindowText(strPsw);
	GetDlgItem(IDC_EDIT_ICON_ID)->GetWindowText(strIconId);
	stMsgUserRegisterReq	stReq;
	memcpy(stReq.szUserName, strName.GetBuffer(0), strName.GetLength());
	memcpy(stReq.szPsw, strPsw.GetBuffer(0), strPsw.GetLength());
	stReq.dwIconId = atoi(strIconId.GetBuffer(0));
	m_oNet.SendMsg(&stReq);
}

void CtestclientDlg::OnBnClickedBtnRestert()
{
	// TODO: Add your control notification handler code here
	stMsgGameReStartReq stMsg;
	m_oNet.SendMsg(&stMsg);
}

void CtestclientDlg::OnBnClickedBtnRestartAck()
{
	// TODO: Add your control notification handler code here
	CString strAck = "";
	GetDlgItem(IDC_EDIT_RESTART_ACK)->GetWindowText(strAck);
	stMsgGameReStartAck stMsg;
	stMsg.byAck = atoi(strAck.GetBuffer(0));
	m_oNet.SendMsg(&stMsg);
}

void CtestclientDlg::OnBnClickedButtonSndReq()
{
	// TODO: Add your control notification handler code here
	CString strExp = "";
	GetDlgItem(IDC_EDIT_EXP)->GetWindowText(strExp);

	stMsgPlayerSndExpReq stMsg;
	stMsg.dwExp = atoi(strExp.GetBuffer(0));
	m_oNet.SendMsg(&stMsg);
}

void CtestclientDlg::OnBnClickedBtnRankReq()
{
	// TODO: Add your control notification handler code here

	stMsgPlayerRankingReq stMsg;
	m_oNet.SendMsg(&stMsg);
}
